const marked = require('marked');
const config = require('config-lite')(__dirname);
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.connect(config.mongodb, { autoIndex: false, useNewUrlParser: true });

var UserSchema = new Schema({
  name: { type: 'string', required: true },
  password: { type: 'string', required: true },
  avatar: { type: 'string', require: true },
  gender: { type: 'string', enum: ['m', 'f', 'x'], default: 'x' },
  bio: { type: 'string', required: true }
})
UserSchema.index({ name: 1 }, { unique: true }); // 根据用户名找到用户，用户名全局唯一

exports.User = mongoose.model('User', UserSchema);

// 1与-1是升序降序规则  根据用户名找到用户，用户名全局唯一
var PostSchema = new Schema({
  author: { type: Schema.Types.ObjectId, ref: 'User', required: true },
  title: { type: 'string', required: true },
  content: { type: 'string', required: true },
  pv: { type: 'number', default: 0 },
  commentsCount: { type: 'number', default: 0 }
})

PostSchema.index({ name: 1 }, { unique: true });

exports.Post = mongoose.model('Post', PostSchema);

var CommentSchema = new Schema({
  author: { type: Schema.Types.ObjectId, required: true },
  content: { type: 'string', required: true },
  postId: { type: Schema.Types.ObjectId, required: true },
})
// 通过文章 id 获取该文章下所有留言，按留言创建时间升序
CommentSchema.index({postId: 1, _id: 1});
exports.Comment = mongoose.model('Comment', CommentSchema);